﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace ltModel
{
    /// <summary>
    ///@tblName varchar(255), -- 表名
    ///@strGetFields varchar(1000) = '*', -- 需要返回的列 
    ///@fldName varchar(255)='', -- 排序的字段名 
    ///@PageSize int = 10, -- 页尺寸 
    ///@PageIndex int = 1, -- 页码 
    ///@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序 
    ///@strWhere varchar(1500) = '' ,-- 查询条件 (注意: 不要加 where) 
    ///@Counts  int  out -- 返回记录总数, 非 0 值则返回 这里作运行后返回的记录总数分页用
    /// </summary>
    /// ----------------------------------------------------------------------------------
    ///@PageSize int=10 ,--每页显示的记录数
    ///@PageCurrent int=1 ,--当前要显示的页号
    ///@FdName varchar(100)='' ,--主键名或者标识列名
    ///@SelectStr varchar(2000)='', --select子句，不包含select关键字,如：*或者Id,UserId,UserName等。
    ///@FromStr varchar(1000)='', --from子句，不包含from关键子，如：myTable或者myTable,yourTable
    ///@WhereStr varchar(2000)='', --Where子句，不包含where关键字，如空的，或者 id>2 等
    ///@OrderByStr varchar(1000)='',--order by 子句，不包含order by 子句 ，如id desc,UserId asc 等
    ///@CountRows int=0 output, --返回记录总数
    ///@CountPage int=0 output --返回总页数
    public class Paging
    {
        #region 初始值
        private int pagesize = 10;//每页显示的记录数
        private int pageindex = 1;//当前要显示的页号
        private string pkname = "ID";//主键名
        private string table;//不包含from关键子，如：myTable或者myTable,yourTable
        private string fields = "*";//不包含select关键字,如：*或者Id,UserId,UserName等
        private string orderby = "News_id DESC";//order by 子句，不包含order by 子句 ，如id desc,UserId asc 等
        private string where = "";//Where子句，不包含where关键字，如空的，或者 id>2 等
        private string url = "";//地址栏参数获取

        private int ordertype = 1;
        private int rcount = 0;
        private string link;
        #endregion

        #region 初始值及属性

        public string PkName
        {
            get { return this.pkname; }
            set { this.pkname = value; }
        }
        /// <summary>
        /// 表名
        /// </summary>
        public string Table
        {
            get { return this.table; }
            set { this.table = value; }
        }
        /// <summary>
        /// 需要返回的列 
        /// </summary>
        public string Fields
        {
            get { return this.fields; }
            set { this.fields = value; }
        }
        /// <summary>
        ///  排序的字段名 
        /// </summary>
        public string OrderBy
        {
            get { return this.orderby; }
            set { this.orderby = value; }
        }
        /// <summary>
        /// 条件
        /// </summary>
        public string Where
        {
            get { return this.where; }
            set { this.where = value; }
        }
        /// <summary>
        /// 每页显示的条数
        /// </summary>
        public int PageSize
        {
            get { return this.pagesize; }
            set
            {
                if (this.pagesize > 1)
                    this.pagesize = value;
                else
                    this.pagesize = 10;
            }
        }
        /// <summary>
        /// 当前页
        /// </summary>
        public int PageIndex
        {
            get { return this.pageindex; }
            set
            {
                if (this.pageindex >= 1)
                    this.pageindex = value;
                else
                    this.pageindex = 1;
            }
        }
        /// <summary>
        /// 0为顺序，1为倒序
        /// </summary>
        public int OrderType
        {
            get { return this.ordertype; }
            set { this.ordertype = value; }
        }
        public string Link
        {
            get { return this.link; }
        }
        /// <summary>
        /// 地址栏参数
        /// </summary>
        public string Url
        {
            get { return url; }
            set { url = value; }
        }
        public int Rcount
        {
            get { return rcount; }
            set { rcount = value; }
        }
        #endregion

        public Paging()
        {
            if (ordertype == 1)
                this.orderby = "News_id DESC";
        }

        public Paging(string table, string fields, string orderby, string pkname, int pagesize, int pageindex, int ordertype, string where)
        {
            this.table = table;
            this.fields = fields;
            //this.orderby = orderby;
            this.pkname = pkname;
            this.pagesize = pagesize;
            this.pageindex = pageindex;
            this.ordertype = ordertype;
            this.where = where;

            if (ordertype == 1)
                this.orderby = "News_id DESC";
        }

        /// <summary>
        /// 返回分页后的记录
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public DataTable GetList()
        {
            if (url == "")
                url = "?";
            else
                url = "?" + url + "&";

            if (HttpContext.Current.Request.QueryString["pageindex"] != null)
            {
                string cix = HttpContext.Current.Request.QueryString["pageindex"].ToString();
                if (PageValidate.IsNumber(cix) == false)
                    this.pageindex = 1;
                else
                    this.pageindex = Int32.Parse(cix);
            }
            int sumcount;//总记录数
            int sumpage;//总页数
            string procName = "PageShowOne";//存储过程名

            /*
            SqlParameter[] para=new SqlParameter[]{
                                                      new SqlParameter("@tblName",this.table),
                                                      new SqlParameter("@strGetFields",this.fields),
                                                      new SqlParameter("@fldName",this.orderby), 
                                                      new SqlParameter("@PageSize",this.pagesize),
                                                      new SqlParameter("@PageIndex",this.pageindex), 
                                                      new SqlParameter("@OrderType",this.ordertype),
                                                      new SqlParameter("@strWhere",this.where),
                                                      new SqlParameter("@Counts",SqlDbType.Int)
														 										  
                                                  };
    ///@PageSize int=10 ,--每页显示的记录数
    ///@PageCurrent int=1 ,--当前要显示的页号
    ///@FdName varchar(100)='' ,--主键名或者标识列名
    ///@SelectStr varchar(2000)='', --select子句，不包含select关键字,如：*或者Id,UserId,UserName等。
    ///@FromStr varchar(1000)='', --from子句，不包含from关键子，如：myTable或者myTable,yourTable
    ///@WhereStr varchar(2000)='', --Where子句，不包含where关键字，如空的，或者 id>2 等
    ///@OrderByStr varchar(1000)='',--order by 子句，不包含order by 子句 ，如id desc,UserId asc 等
    ///@CountRows int=0 output, --返回记录总数
    ///@CountPage int=0 output --返回总页数
            */
            SqlParameter[] para = new SqlParameter[]{
													  new SqlParameter("@PageSize",this.pagesize),
													  new SqlParameter("@PageCurrent",this.pageindex), 
													  new SqlParameter("@FdName",this.pkname),
													  new SqlParameter("@SelectStr",this.fields),
													  new SqlParameter("@FromStr",this.table),
													  new SqlParameter("@WhereStr",this.where),
													  new SqlParameter("@OrderByStr",this.orderby), 
													  new SqlParameter("@CountRows",SqlDbType.Int),
													  new SqlParameter("@CountPage",SqlDbType.Int)
														 										  
												  };

            para[7].Direction = ParameterDirection.Output;


            DataTable dt = new DataTable();
            dt = DataObject.RunProcedure(procName, para, "article_content").Tables[0];

            sumcount = Convert.ToInt32(para[7].Value);//取得总记录数
            rcount = sumcount;
            sumpage = (int)Math.Ceiling((double)sumcount / pagesize);//计算分成多少页

            int iStart = pageindex - 3;
            int iEnd = pageindex + 2;

            if (iStart < 0) iStart = 0;
            if (iEnd > sumpage) iEnd = sumpage;


            if (sumpage > 1)
            {
                string gourl = "<input type=\"text\" id=\"txturl\" value=\"" + this.pageindex + "\" style=\"width:30px;\" /><input type=\"button\" onclick=\"gotourl();\" value=\" GO \" />";
                string fpage = "<a href=\"" + url + "pageindex=1\" {0}><font face=\"webdings\" title='转到第一页'>9</font></a>&nbsp;";//第一页

                string npage = "<a href=\"" + url + "pageindex=" + Convert.ToString(this.pageindex + 1) + "\" {0} title='转到下一页'><font face=\"webdings\">4</font></a>&nbsp;";//下一页
                string ppage = "<a href=\"" + url + "pageindex=" + Convert.ToString(this.pageindex - 1) + "\" {0} title='转到上一页'><font face=\"webdings\">3</font></a>&nbsp;";//上一页
                string lpage = "<a href=\"" + url + "pageindex=" + Convert.ToString(sumpage) + "\" {0} title='转到最后一页'><font face=\"webdings\">:</font></a>&nbsp;&nbsp;";//最后一页

                link = "共<b>" + sumcount.ToString() + "</b>条记录,分成<b>" + sumpage.ToString() + "</b>页,当前第<b>" + pageindex.ToString() + "</b>页&nbsp;";

                if (this.pageindex == 1)
                {
                    //link += string.Format(fpage,"disabled=\"true\"");//第一页
                    //link +=string.Format(ppage,"disabled=\"true\"");// + gourl;
                    //link +=
                    for (int i = iStart; i < iEnd; i++)
                    {
                        if ((i + 1) == pageindex)
                            link += "<u>" + Convert.ToString(i + 1) + "</u>&nbsp;";
                        else
                            link += "<a href=\"" + url + "pageindex=" + Convert.ToString(i + 1) + "\" title=\"跳转到第" + Convert.ToString(i + 1) + "页\"><b>" + Convert.ToString(i + 1) + "</b></a>&nbsp;";
                    }
                    link += string.Format(npage, "");
                    link += string.Format(lpage, "");
                    link += gourl;

                }
                else if (this.pageindex == sumpage)
                {
                    link += string.Format(fpage, "");//第一页
                    link += string.Format(ppage, "");// + gourl;
                    //link +=
                    for (int i = iStart; i < iEnd; i++)
                    {
                        if ((i + 1) == pageindex)
                            link += "<u>" + Convert.ToString(i + 1) + "</u>&nbsp;";
                        else
                            link += "<a href=\"" + url + "pageindex=" + Convert.ToString(i + 1) + "\" title=\"跳转到第" + Convert.ToString(i + 1) + "页\"><b>" + Convert.ToString(i + 1) + "</b></a>&nbsp;";
                    }
                    //link+=string.Format(npage,"");
                    //link+=string.Format(lpage,"");
                    link += gourl;
                }
                else
                {
                    link += string.Format(fpage, "");//第一页
                    link += string.Format(ppage, "");// + gourl;
                    //link +=
                    for (int i = iStart; i < iEnd; i++)
                    {
                        if ((i + 1) == pageindex)
                            link += "<u>" + Convert.ToString(i + 1) + "</u>&nbsp;";
                        else
                            link += "<a href=\"" + url + "pageindex=" + Convert.ToString(i + 1) + "\" title=\"跳转到第" + Convert.ToString(i + 1) + "页\"><b>" + Convert.ToString(i + 1) + "</b></a>&nbsp;";
                    }
                    link += string.Format(npage, "");
                    link += string.Format(lpage, "");
                    link += gourl;

                }
                link += @"<script>function gotourl(){var pageindex=document.getElementById('txturl').value;";
                link += @"var pattern=/^\d+$/;";
                link += @"if(!pattern.test(pageindex) || pageindex.length==0 || pageindex=='' || pageindex<=0){alert('请输入一个大与0的整数！');return false;}";
                link += @"if(pageindex>" + sumpage + "){alert('索引超出范围！');return false;}";
                link += @"window.location.href='" + url + "pageindex='+pageindex;}</script>";

            }
            return dt;
        }
    }
}